package com.resou.reader.tinder.carddetail;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.app.AppCompatActivity;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.resou.reader.R;
import com.resou.reader.api.ApiConstant;
import com.resou.reader.bookdetail.BookDetailActivity;
import com.resou.reader.data.tinder.tinder.SlideBean;
import com.resou.reader.tinder.favorites.FavoritesActivity;
import com.resou.reader.mine.IView.UserInstance;
import com.resou.reader.utils.EncryptUtils;
import com.resou.reader.utils.StatusBarUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CardDetailActivity extends AppCompatActivity implements ICardDetailView {

    public static final String BOOK_BEAN = "book_bean";
    @BindView(R.id.tv_toolbar_title)
    TextView tvToolbarTitle;
    @BindView(R.id.toolbar)
    FrameLayout toolbar;
    @BindView(R.id.tv_finish)
    TextView tvFinish;
    @BindView(R.id.tv_content)
    TextView tvContent;

    @BindView(R.id.scroll_view)
    NestedScrollView scrollView;
    @BindView(R.id.tv_chapter_name)
    TextView tvChapterName;
    @BindView(R.id.tv_favorites_count)
    TextView tvFavoritesCount;

    private StatusBarUtil statusBarUtil = StatusBarUtil.getInstance();
    private SlideBean mBook;
    private String mToken;
    private CardDetailPresenter mPresenter;

    public static void start(Context context, SlideBean slideBean) {
        Intent starter = new Intent(context, CardDetailActivity.class);
        starter.putExtra(BOOK_BEAN, slideBean);
        context.startActivity(starter);
    }

    public static Intent getStartIntent(Context context, SlideBean slideBean) {
        Intent intent = new Intent(context, CardDetailActivity.class);
        intent.putExtra(BOOK_BEAN, slideBean);
        return intent;
    }

    public static final int RESULT_LIKE = 1;
    public static final int RESULT_DISLIKE = 2;

    @OnClick(R.id.iv_nope)
    void doNope() {
        // 不再提示
        setResult(RESULT_DISLIKE);
        onBackPressed();
    }

    @OnClick(R.id.iv_like)
    void doLike() {
        // 添加到喜欢列表
        setResult(RESULT_LIKE);
        onBackPressed();
    }

    @OnClick(R.id.iv_favorites)
    void startToFavoritesActivity() {
        FavoritesActivity.startActivity(this);
    }

    @OnClick(R.id.iv_read)
    void doRead() {
        BookDetailActivity.startDetailActivity(this, mBook.getBookId());
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_card_detail);
        ButterKnife.bind(this);
        statusBarUtil.setUpStatusBar(this).build();

        mPresenter = new CardDetailPresenter(this);
        mToken = UserInstance.getToken();
        if (TextUtils.isEmpty(mToken)) {
           return;
        }

        mBook = getIntent().getParcelableExtra(BOOK_BEAN);
        tvToolbarTitle.setText(mBook.getNovelName());
        tvChapterName.setText(mBook.getChapterName());
        try {
            String decrypt = EncryptUtils.decrypt(mBook.getContent(), mBook.getBookId(), mBook.getChapterId());
            String content = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + decrypt;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                tvContent.setText(Html.fromHtml(content, Html.FROM_HTML_MODE_COMPACT));
            } else {
                tvContent.setText(Html.fromHtml(content));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        tvFinish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mPresenter.getCartCount(mToken);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPresenter.unsubscribe();
    }

    @Override
    public void showFavoritesCount(String count) {
        tvFavoritesCount.setText(count);
    }

    @Override
    public void initData() {

    }

    @Override
    public void showContent() {

    }

    @Override
    public void showProgress() {

    }

    @Override
    public void showError(Throwable throwable) {

    }

    @Override
    public void showEmpty() {

    }
}
